import requests
import json
import string
import random
import time

#测试数据（调整类）
data = {}

#生产数据
wd = {}

class request_test():
    data1 = json.dumps(data)

    def post_requests(self, for_num, *args):
        #测试环境
        token = ''
        url = ''

        #生产环境
        # token = ''
        # url = ''

        status = '1'
        num = 1
        error_num = 1
        self.consumDt_2 = []

        while status == '1':
            i = 1
            print("###################准备请求信息###################")
            while i <= for_num:
                consumDt_0 = {}
                wd.update({"gdsMtno": ''.join(random.sample(string.ascii_letters + string.digits, 8))})
                consumDt_0.update(wd)
                self.consumDt_2.append(consumDt_0)
                i += 1

            data = request_test.data1.replace("wd1", json.dumps(self.consumDt_2)).replace('"[', '[').replace(']"', ']')
            try:
                headers = {"Content-Type": "application/json", "epToken": "%s" % token}
                # print(data)
                # response = [requests.post(url=url, headers=headers, data=data) for i in range(100)]
                while error_num <= 3:
                    date1 = time.strftime('%Y%m%d%H%M%S')
                    t1 = time.time()
                    response = requests.post(url=url, headers=headers, data=data)
                    status = response.json().get("status")
                    ass_value = response.json()
                    t2 = time.time()
                    if status == "1":
                        print("=================单证生成===================")
                        print('第%s次尝试请求成功' % num)
                        id = ass_value.get("map").get('data')
                        print('生成单证id：%s\n 当前单证表体条数：%s' % (id, for_num))
                        error_num = 0
                        break
                    else:
                        print("=================异常反馈===================")
                        print('第%s次请求失败' % num)
                        print('请求时长：%s' % (t2-t1))
                        if 'token cather user fail' in str(ass_value):
                            print("token失效")
                            break
                        elif status == "0":
                            print('失败返回信息：' + ass_value.get("note"))
                        elif status == '404':
                            print('请求地址错误：'+str(ass_value))
                            break
                        else:
                            print('详细错误信息：'+str(ass_value))
                            print("当前最大支持表体数：%s" % (for_num - 1))
                        error_num += 1
                    num += 1
            except Exception as E:
                print("检查请求信息")
                print(E)
                print(ass_value)
                return False, E, "请求异常"
            if len(args) > 0 and error_num < 3:
                max_num = args[0]
                status = "1"
                if for_num < max_num:
                    for_num += 1
                else:
                    status = '0'
            else:
                status = '0'
                print(for_num)
            print('状态值：%s' % status)
            print("###################请求结束###################")
            num += 1

# 输入预期表体数量或预期请求的下限、上限
request_test().post_requests(1000)